(function ($) {
    $.fn.yqTextRichBox = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqTextRichBox.methods[opts](this, param);
        }

        $this = this;
        var opt = $.extend({}, $.fn.yqTextRichBox.defaults, opts);

        var $uniqueID = $this.attr("ID");
        if ($uniqueID === undefined) {
            var id = 'yyxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = Math.random() * 16 | 0,
                    v = c == 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            });

            $this.attr("ID", id);
        }

        if (opt.width === undefined && $this.width() > 0) {
            opt.width = $this.width();
        }
        else if (typeof opt.width != "undefined") {
            $this.width(opt.width);
        }

        opt.onload = true;

        //变量信息
        $this.data("texttichbox_opt", opt);
        $this.data("yqtype", "yqTextRichBox");

        init_text($this);
    };

    $.fn.yqTextRichBox.defaults = {
        value: null,
        blank: true,
        min: null,
        max: null,
        types: ["base"],
        type: "base",
        isEdit: true,   //可编辑
        editModeW: 400, //模态编辑器宽度 格式为 800（数字）
        editModeH: 300, //模态编辑器高度 格式为 600（数字）
        beforeEditShow: function (edit_type, options, callback) {
            callback(true, edit_type, options);
        }, //弹出框显示前
        afterChange: function (value) {

        }
    };

    $.fn.yqTextRichBox.methods = {
        options: function (jq) {
            return options(jq);
        },
        destroy: function (jq) {
            destroy(jq);
        },
        resize: function (jq, width) {
            resize(jq, width);
        },
        clear: function (jq, data) {
            setValue(jq, null);
        },
        setValue: function (jq, value) {
            setValue(jq, value);
        },
        getValue: function (jq) {
            return getValue(jq);
        },
        isEdit: function (jq, tf) {
            isEdit(jq, tf);
        },
    };

    function options($object) {
        return $object.data("texttichbox_opt");
    }

    function destroy($object) {
        $object.textbox("destroy");
    }

    function resize($object, width) {
        $object.textbox("resize", width);
    }

    function init_text($object) {
        var opt = options($object);
        var $uniqueID = $object.attr('id');

        var text_opt = {
            editable: false,
            iconAlign: 'right',
            icons: [{
                iconCls: 'yqwin-combo-btn-icon fa fa-pencil',
                handler: function (e) {
                    showEdit();
                }
            }, {
                iconCls: 'yqwin-combo-btn-icon fa fa-times',
                //disabled:true,
                handler: function (e) {
                    setValue($object, null);

                    opt.afterChange.call($object, null);
                }
            }]
        };

        $object.textbox(text_opt);
        $object.textbox("textbox").bind("click", function () {
            showEdit();
        });


        setValue($object, opt.value);
        isEdit($object, opt.isEdit);

        //启动编辑界面
        function showEdit() {
            opt.beforeEditShow.call($object, "yqTextRichBox", {
                value: opt.value,
                blank: opt.blank,
                min: opt.min,
                max: opt.max,
                types: opt.types,
                type: opt.type,
                isEdit: opt.isEdit,   //可编辑
                editModeW: opt.editModeW, //模态编辑器宽度 格式为 800（数字）
                editModeH: opt.editModeH, //模态编辑器高度 格式为 600（数字）
            }, function (tf, edit_type, options) {
                if (!tf) {
                    return;
                }

                var pp = $object.textbox("textbox");
                prevent_e = $.getPreventDefault(pp[0]);
                prevent_e.id = $object.attr("id");
                prevent_e.options = opt;

                $.pop_yqEditor[edit_type].call($object, prevent_e, options);
            });
        }
    }

    function setValue($object, value) {
        var opt = options($object);
        if (value === undefined || value == "" || value == null) {
            value = null;
        } else if (typeof value == "object" && !(value.value === undefined)) {
            value = value.value;
        }
        opt.value = value;
        $object.textbox("setValue", value);
    }

    function getValue($object) {
        var opt = options($object);
        return opt.value;
    }

    function isEdit($object, tf) {
        var opt = options($object);
        var icon0 = $object.textbox("getIcon", 0);
        var icon1 = $object.textbox("getIcon", 1);
        opt.isEdit = tf;
        if (opt.isEdit) {
            icon0.removeClass('icon-more');
            icon0.addClass('icon-edit');
            icon1.show();
            //icon1.removeClass('textbox-icon-disabled');
        } else {
            icon0.removeClass('icon-edit');
            icon0.addClass('icon-more');
            //icon1.addClass('textbox-icon-disabled');
            icon1.hide();
        }
    }
})(jQuery);
